﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace HY.Web
{
    /// <summary>
    /// MvcHtmlHelper扩展
    /// </summary>
    public static class HtmlHelperExtension
    {
        /// <summary>
        /// 按钮扩展
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="id">按钮ID</param>
        /// <param name="text">按钮名称</param>
        /// <param name="cssClass">按钮样式</param>
        /// <param name="url">对应区域名称</param>
        /// <param name="htmlAttributes">自定义属性</param>
        /// <returns></returns>
        public static MvcHtmlString Button(this HtmlHelper htmlHelper, string id, string text, string cssClass, string url, object htmlAttributes = null)
        {
            if ((!string.IsNullOrWhiteSpace(url)) && GlobalSupport.CheckAuthorize(url))
            {
                var builder = new TagBuilder("button");
                builder.IdAttributeDotReplacement = "-";
                builder.GenerateId(id);
                if (htmlAttributes != null)
                    builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
                builder.AddCssClass(cssClass);
                builder.SetInnerText(text);
                return new MvcHtmlString(builder.ToString(TagRenderMode.Normal));
            }
            return new MvcHtmlString(string.Empty);
        }

        /// <summary>
        /// 按钮扩展
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="id">id,同时赋值给lay-event属性</param>
        /// <param name="text">按钮名称</param>
        /// <param name="cssClass">按钮样式</param>
        /// <param name="url">菜单链接</param>
        /// <returns></returns>
        public static MvcHtmlString LayuiButton(this HtmlHelper htmlHelper, string id, string text, string cssClass, string url)
        {
            string htmlStr = string.Empty;
            if ((!string.IsNullOrWhiteSpace(url)) && GlobalSupport.CheckAuthorize(url))
            {
                htmlStr = $"<a class=\"{cssClass}\" id=\"{id}\" lay-event=\"{id}\">{text}</a>";
            }
            return new MvcHtmlString(htmlStr);
        }
    }
}